//
//  HQEditSearchViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/18.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQEditSearchViewController.h"
#import "MagicImageView.h"
#import "HQBigImageViewController.h"

@interface HQEditSearchViewController ()<UITextFieldDelegate>
{
    NSMutableArray *_imageList;
    __weak IBOutlet NSLayoutConstraint *_bottomViewConstraint;
}
@end

@implementation HQEditSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"搜索样式";
    
    _jiaGeHeSuanTag = NO;
    _dataSource = [[NSMutableArray alloc]init];
    
    _hesuanjiageButton.hidden = YES;
    _searchButton2.hidden = YES;
    
    _yangShiMingCheng.text = [NSString stringWithFormat:@"  %@  ",self.dataInfo[@"s_name"]];
    _yangshiJieShao.text = self.dataInfo[@"s_introduce"];
    [_topImage setImageWithURL:[NSURL URLWithString:self.dataInfo[@"s_img"]]];
    
//    MagicImageView *igv = [[MagicImageView alloc] initWithImage:[UIImage imageNamed:@"pic"]];
//    igv.frame = CGRectMake(100, 100, 100, 100);
//    [self.view addSubview:igv];
    
    [self addKeyBoardObserver];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    _bottomViewConstraint.constant = keyboardRect.size.height;
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    _bottomViewConstraint.constant = 0;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(_dataSource.count > 0){
        return 2;
    }else{
        return 1;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return _dataSource.count;
    }else{
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        HQSearchObject *obj = _dataSource[indexPath.row];
        if([obj.itemTag  isEqualToString:@"DELETECELL"]){
            HQStyleUserSearchEditCell *cell = [HQStyleUserSearchEditCell cellWithTableView:tableView];
            cell.dataInfo = _dataSource[indexPath.row];
            cell.deleteItem.dataInfo = cell.dataInfo;
            cell.tag = 1000+indexPath.row;
            [cell.mingCheng addTarget:self action:@selector(selectMC:) forControlEvents:UIControlEventTouchUpInside];
            [cell.mingCheng2 addTarget:self action:@selector(selectMC:) forControlEvents:UIControlEventTouchUpInside];
            [cell.deleteItem addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
            [cell.guiGe addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            [cell.danJia addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            [cell.huoHao addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            return cell;
        }else{
            HQStyleUserSearchCell *cell = [HQStyleUserSearchCell cellWithTableView:tableView];
            cell.tag = 1000+indexPath.row;
            cell.dataInfo = _dataSource[indexPath.row];
            [cell.danJia addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            [cell.huoHao addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
            return cell;
        }
    }else{
        static NSString *cellId = @"photo";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if(!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.backgroundColor = [UIColor clearColor];
            CGFloat width = self.view.frame.size.width;
            HXPhotoView *photoView = [HXPhotoView photoManager:self.manager];
            photoView.frame = CGRectMake(kPhotoViewMargin, kPhotoViewMargin, width - kPhotoViewMargin * 2, 260);
            photoView.delegate = self;
            photoView.outerCamera = YES;
            photoView.previewShowDeleteButton = YES;
            //    photoView.hideDeleteButton = YES;
            photoView.showAddCell = YES;
            [photoView.collectionView reloadData];
            photoView.backgroundColor = [UIColor clearColor];
            [cell addSubview:photoView];
            self.photoView = photoView;
            //            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"相册/相机" style:UIBarButtonItemStylePlain target:self action:@selector(didNavBtnClick)];
            [cell addSubview:self.bottomView];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

/**
 文本框输入控制
 */
-(void)textChange{
    int i =0;
    for(HQSearchObject *sty in _dataSource){
        if([sty.itemTag isEqualToString:@"DELETECELL"]){
            HQStyleUserSearchEditCell * cell = [_tableView viewWithTag:1000+i];
            sty.danJia = cell.danJia.text;
            sty.huoHao = cell.huoHao.text;
            sty.m_material = cell.guiGe.text;
            if(_jiaGeHeSuanTag){
                sty.isOpenHeSuan = @"YES";
            }else{
                sty.isOpenHeSuan = @"NO";
            }
        }else{
            HQStyleUserSearchCell * cell = [_tableView viewWithTag:1000+i];
            sty.danJia = cell.danJia.text;
            sty.huoHao = cell.huoHao.text;
            if(_jiaGeHeSuanTag){
                sty.isOpenHeSuan = @"YES";
            }else{
                sty.isOpenHeSuan = @"NO";
            }
        }
        i++;
    }
    if(_jiaGeHeSuanTag){
        float allMoney = 0.0;
        for(int i = 0;i<_dataSource.count;i++){
            UITableViewCell * cell = [_tableView viewWithTag:1000+i];
            HQSearchObject *sty = _dataSource[i];
            if([cell isKindOfClass:[HQStyleUserSearchCell class]]){
                HQStyleUserSearchCell *d1Cell = (HQStyleUserSearchCell *)cell;
                sty.danJia = d1Cell.danJia.text;
                sty.guiGe = sty.price;
            }else if([cell isKindOfClass:[HQStyleUserSearchEditCell class]]){
                HQStyleUserSearchEditCell *d2Cell = (HQStyleUserSearchEditCell *)cell;
                sty.danJia = d2Cell.danJia.text;
                sty.guiGe = d2Cell.guiGe.text;
            }
            float temNum = [sty.guiGe floatValue] * [sty.danJia floatValue];
            allMoney = allMoney + temNum;
        }
        _heSuanZongE = [NSString stringWithFormat:@"%.2f",allMoney];
        [_bottomZongEItem setTitle:[NSString stringWithFormat:@"总额：%@元",_heSuanZongE] forState:UIControlStateNormal];
//        [_hesuanjiageButton setTitle:[NSString stringWithFormat:@"总额：%@元",_heSuanZongE] forState:UIControlStateNormal];
    }
}


/**
 选择样式
 
 @param button 样式
 */
-(void)selectMC:(HQButton *)button{
    _currentCell = button.className;
    // 列表中有值，且已经输入过数据
    [self.view endEditing:YES];
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam allComponents:[UserModelManager userID]] withMethod:[HQMethod compontIndex] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSArray *data = sucData[@"data"];
            NSMutableArray *m = [[NSMutableArray alloc]init];
            _styleItem0Array = [[NSMutableArray alloc]init];
            for(NSDictionary *dic in data){
                [_styleItem0Array addObject:dic];
                [m addObject:[NSString stringWithFormat:@"%@",dic[@"c_name"]]];
            }
            ZHPickView *_zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
            [_zhPickerView setPickeTitle:@"样式组件"];
            [_zhPickerView selectObj:_currentCell.mingCheng.titleLabel.text];
            _zhPickerView.delegate = self;
            _zhPickerView.tag = 1004;
            [_zhPickerView show];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 保存当前数据
 
 @param sender 按钮样式
 */
- (IBAction)saveCurrentData:(id)sender {
    if([self isInputEmpty:_chuangLianChiCun.text]){
        [self tostView:@"请输入窗帘尺寸"];
        return;
    }else if([self isInputEmpty:_guKeXingMing.text]){
        [self tostView:@"请输入顾客姓名"];
        return;
    }else if([self isInputEmpty:_guKeShouJiHao.text]){
        [self tostView:@"请输如顾客手机号"];
        return;
    }else if(![HQTool phoneNoRegex:_guKeShouJiHao.text]){
        [self tostView:@"请输入正确的手机号"];
        return;
    }else if(_dataSource.count == 0){
        [self tostView:@"您没有添加任何规格数据"];
        return;
    }
    // 校验界面中未填写项
    // 校验核算结果是否有数据
    if(_jiaGeHeSuanTag){
        if([_heSuanZongE integerValue] == 0){
            [self tostView:@"请核算价格后提交"];
            return;
        }
    }else{
        [self tostView:@"请核算价格后提交"];
        return;
    }
    
    // 校验列表中未填项
    for(int i =0;i<_dataSource.count;i++){
        UITableViewCell * cell = [_tableView viewWithTag:1000+i];
        if(_jiaGeHeSuanTag){
            // 价格核算校验项较多
            if([cell isKindOfClass:[HQStyleUserSearchCell class]]){
                HQStyleUserSearchCell *d1Cell = (HQStyleUserSearchCell *)cell;
                if([self isInputEmpty:d1Cell.danJia.text] ||[self isInputEmpty:d1Cell.huoHao.text]){
                    [self tostView:@"界面中有未填项,请填写后提交"];
                    return;
                }
            }else if([cell isKindOfClass:[HQStyleUserSearchEditCell class]]){
                HQStyleUserSearchEditCell *d2Cell = (HQStyleUserSearchEditCell *)cell;
                if([d2Cell.mingCheng.titleLabel.text isEqualToString:@"选择"] || [self isInputEmpty:d2Cell.danJia.text] || [self isInputEmpty:d2Cell.huoHao.text]||[self isInputEmpty:d2Cell.guiGe.text]){
                    [self tostView:@"界面中有未填项,请填写后提交"];
                    return;
                }
            }
        }else{
            if([cell isKindOfClass:[HQStyleUserSearchEditCell class]]){
                HQStyleUserSearchEditCell *d2Cell = (HQStyleUserSearchEditCell *)cell;
                if([self isInputEmpty:d2Cell.guiGe.text]||[d2Cell.mingCheng.titleLabel.text isEqualToString:@"选择"]){
                    [self tostView:@"界面中有未填项,请填写后提交"];
                    return;
                }
            }
        }
    }
    
    //    //    //    //    //    //  有价格核算的情况下提交数据   //    //    //    //    //    //    //
    NSMutableString *zuJianIDArr = [[NSMutableString alloc]init];
    NSMutableString *huoHaoArr = [[NSMutableString alloc]init];
    NSMutableString *yongLiaoArr = [[NSMutableString alloc]init];
    NSMutableString *danJiaArr = [[NSMutableString alloc]init];
    for(int i = 0;i<_dataSource.count;i++){
        UITableViewCell * cell = [_tableView viewWithTag:1000+i];
        HQSearchObject *sty = _dataSource[i];
        if([cell isKindOfClass:[HQStyleUserSearchCell class]]){
            HQStyleUserSearchCell *d1Cell = (HQStyleUserSearchCell *)cell;
            sty.danJia = d1Cell.danJia.text;
            sty.huoHao = d1Cell.huoHao.text;
            sty.guiGe = sty.price;
        }else if([cell isKindOfClass:[HQStyleUserSearchEditCell class]]){
            HQStyleUserSearchEditCell *d2Cell = (HQStyleUserSearchEditCell *)cell;
            sty.danJia = d2Cell.danJia.text;
            sty.huoHao = d2Cell.huoHao.text;
            sty.guiGe = d2Cell.guiGe.text;
        }
        [zuJianIDArr appendFormat:@"%@,",sty.c_name];
        [huoHaoArr appendFormat:@"%@,",sty.huoHao];
        [yongLiaoArr appendFormat:@"%@%@,",sty.guiGe,sty.c_unit];
        [danJiaArr appendFormat:@"%@,",sty.danJia];
//        [danJiaArr appendFormat:@"%@元/%@,",sty.danJia,sty.c_unit];
    }
    
    NSString *zuJianIDArrStr = @"";
    NSString *huoHaoArrStr = @"";
    NSString *yongLiaoArrStr = @"";
    NSString *danJiaArrStr = @"";
    if(zuJianIDArr.length>0){
        zuJianIDArrStr = [zuJianIDArr substringToIndex:zuJianIDArr.length - 1];
        huoHaoArrStr = [huoHaoArr substringToIndex:huoHaoArr.length - 1];
        yongLiaoArrStr = [yongLiaoArr substringToIndex:yongLiaoArr.length - 1];
        danJiaArrStr = [danJiaArr substringToIndex:danJiaArr.length - 1];
    }
    
    NSLog(@"需要核算时: \r组件%@ \r货号%@ \r用料%@ \r单价%@",zuJianIDArrStr,huoHaoArrStr,yongLiaoArrStr,danJiaArrStr);
    //   //    //    //    //    //    //  没有价格核算的情况下提交数据   //    //    //    //    //    //    //
    if(!_jiaGeHeSuanTag){
        _heSuanZongE = @"";
        danJiaArrStr = @"";
        huoHaoArrStr = @"";
    }
    
    NSLog(@"窗帘尺寸 %@ 顾客姓名%@ 顾客手机号%@最后提交的数据是: \r组件%@ \r货号%@ \r用料%@ \r单价%@ ===总额 %@",_chuangLianChiCun.text,_guKeXingMing.text,_guKeShouJiHao.text,zuJianIDArrStr,huoHaoArrStr,yongLiaoArrStr,danJiaArrStr,_heSuanZongE);
        
    NSMutableArray *imgArr = [[NSMutableArray alloc]init];
    if(_imageList.count>0){
        for(UIImage *img in _imageList){
            [self showHudText:@"上传图片..."];
            HQNetWork *hqnetwork = [[HQNetWork alloc]init];
            [hqnetwork httpUploadImages:img URL:[HQMethod uploadface] param:nil suc:^(NSDictionary *suc) {
                [self hiddnHud];
                if([suc[kStatus]integerValue] == 1){
                    NSString *imageUrl = [[suc objectForKey:@"data"] objectForKey:@"imgurl"];
                    [imgArr addObject:imageUrl];
                    if(imgArr.count == _imageList.count){
                        // 图片接口
                        NSArray *images = imgArr;
                        // 最后提交数据
                        [self showHudText:@"提交数据中..."];
                        [HQNetWork requetParam:[HQParam stylecomFindAddwithUID:[UserModelManager userID] // 用户ID
                                                                       withSID:self.dataInfo[@"s_id"] // 样式ID
                                                                      withName:_guKeXingMing.text // 顾客姓名
                                                                     withPhone:_guKeShouJiHao.text // 顾客手机号
                                                                withCountPrice:_heSuanZongE // 总价
                                                                    withOprice:danJiaArrStr // 单价
                                                                       withEYl:yongLiaoArrStr // 用料数量
                                                                       withEZJ:zuJianIDArrStr // 组件名称
                                                                    withOCarGo:huoHaoArrStr // 货号
                                                                        images:images
                                                ]withMethod:[HQMethod findAddStyle] suc:^(NSDictionary *sucData) {
                            [self hiddnHud];
                            if([sucData[kStatus]integerValue] == 1){
                                // 用户下单成功
                                [self tostView:@"下单成功，请到订单界面查看订单"];
                                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                    [self.navigationController popViewControllerAnimated:YES];
                                });
                            }else{
                                [self tostView:sucData[kInfo]];
                            }
                        } fail:^{
                            [self hiddenErrorHud];
                        }];
                    }
                }else{
                    [self tostView:suc[kInfo]];
                }
            } fail:^{
                [self hiddenErrorHud];
            }];
        }
    }else{
        // 图片接口
        NSArray *images = imgArr;
        // 最后提交数据
        [self showHudText:@"提交数据中..."];
        [HQNetWork requetParam:[HQParam stylecomFindAddwithUID:[UserModelManager userID] // 用户ID
                                                       withSID:self.dataInfo[@"s_id"] // 样式ID
                                                      withName:_guKeXingMing.text // 顾客姓名
                                                     withPhone:_guKeShouJiHao.text // 顾客手机号
                                                withCountPrice:_heSuanZongE // 总价
                                                    withOprice:danJiaArrStr // 单价
                                                       withEYl:yongLiaoArrStr // 用料数量
                                                       withEZJ:zuJianIDArrStr // 组件名称
                                                    withOCarGo:huoHaoArrStr // 货号
                                                        images:images
                                ]withMethod:[HQMethod findAddStyle] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus]integerValue] == 1){
                // 用户下单成功
                [self tostView:@"下单成功，请到订单界面查看订单"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }
}

/************************************************价格核算相关逻辑************************************************/
/**
 价格核算
 
 @param sender 按钮
 */
- (IBAction)priceHeSuan:(UIButton *)sender {
    if(!_jiaGeHeSuanTag){
        _jiaGeHeSuanTag =!_jiaGeHeSuanTag;
    }
    if(_jiaGeHeSuanTag){
        [sender setTitle:@"价格核算中..." forState:UIControlStateNormal];
        for(HQSearchObject *s in _dataSource){
            s.isOpenHeSuan = @"YES";
        }
    }
    [_tableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return 44;
    }else{
        return SCREEN_WIDTH + 32;
    }
}

// 显示核算结果视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(section == 0){
        if(_dataSource.count>0){
            HeSuanHeaderView *h = [HeSuanHeaderView loadTopHotView];
            h.heSuanBgView.hidden = YES;
            h.delegate = self;
            h.heSuanConstraint.constant = 0;
            return h;
        }else{
            return 0;
        }
    }else{
        return 0;
    }
}

// 显示核算结果视图
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section == 0){
        if(_dataSource.count >0){
            return 70;
        }else{
            return 0;
        }
    }else{
        return 0;
    }
}

/**
 核算价格代理方法 核算需要输入价格
 */
-(void)heSuanJiaGe{
    if([self isInputEmpty:_chuangLianChiCun.text]){
        [self tostView:@"请输入窗帘尺寸"];
        return;
    }else if([self isInputEmpty:_guKeXingMing.text]){
        [self tostView:@"请输入顾客姓名"];
        return;
    }else if([self isInputEmpty:_guKeShouJiHao.text]){
        [self tostView:@"请输如顾客手机号"];
        return;
    }else if(![HQTool phoneNoRegex:_guKeShouJiHao.text]){
        [self tostView:@"请输入正确的手机号"];
        return;
    }else if(_dataSource.count == 0){
        [self tostView:@"您没有添加任何规格数据"];
        return;
    }
    // 校验列表中未填项
    for(int i =0;i<_dataSource.count;i++){
        UITableViewCell * cell = [_tableView viewWithTag:1000+i];
        if(_jiaGeHeSuanTag){
            // 价格核算校验项较多
            if([cell isKindOfClass:[HQStyleUserSearchCell class]]){
                HQStyleUserSearchCell *d1Cell = (HQStyleUserSearchCell *)cell;
                if([self isInputEmpty:d1Cell.danJia.text] ||[self isInputEmpty:d1Cell.huoHao.text]){
                    [self tostView:@"界面中有未填项,请填写后提交"];
                    return;
                }
            }else if([cell isKindOfClass:[HQStyleUserSearchEditCell class]]){
                HQStyleUserSearchEditCell *d2Cell = (HQStyleUserSearchEditCell *)cell;
                if([d2Cell.mingCheng.titleLabel.text isEqualToString:@"选择"] || [self isInputEmpty:d2Cell.danJia.text] || [self isInputEmpty:d2Cell.huoHao.text]||[self isInputEmpty:d2Cell.guiGe.text]){
                    [self tostView:@"界面中有未填项,请填写后提交"];
                    return;
                }
            }
        }else{
            if([cell isKindOfClass:[HQStyleUserSearchEditCell class]]){
                HQStyleUserSearchEditCell *d2Cell = (HQStyleUserSearchEditCell *)cell;
                if([self isInputEmpty:d2Cell.guiGe.text]||[d2Cell.mingCheng.titleLabel.text isEqualToString:@"选择"]){
                    [self tostView:@"界面中有未填项,请填写后提交"];
                    return;
                }
            }
        }
    }
    //    //    //    //    //    //   价格核算主要逻辑   //    //    //    //    //    //    //
    float allMoney = 0.0;
    for(int i = 0;i<_dataSource.count;i++){
        UITableViewCell * cell = [_tableView viewWithTag:1000+i];
        HQSearchObject *sty = _dataSource[i];
        if([cell isKindOfClass:[HQStyleUserSearchCell class]]){
            HQStyleUserSearchCell *d1Cell = (HQStyleUserSearchCell *)cell;
            sty.danJia = d1Cell.danJia.text;
            sty.huoHao = d1Cell.huoHao.text;
            sty.guiGe = sty.price;
        }else if([cell isKindOfClass:[HQStyleUserSearchEditCell class]]){
            HQStyleUserSearchEditCell *d2Cell = (HQStyleUserSearchEditCell *)cell;
            sty.danJia = d2Cell.danJia.text;
            sty.huoHao = d2Cell.huoHao.text;
            sty.guiGe = d2Cell.guiGe.text;
        }
        float temNum = [sty.guiGe integerValue] * [sty.danJia floatValue];
        allMoney = allMoney + temNum;
    }
    _heSuanZongE = [NSString stringWithFormat:@"%.2f",allMoney];
    _allMoneyText.text = [NSString stringWithFormat:@"%@元",_heSuanZongE];
}

/************************************************添加自定义组件数据************************************************/
/**
 添加一条自定义样式数据
 */
-(void)addCell{
    int i =0;
    for(HQSearchObject *sty in _dataSource){
        if([sty.itemTag isEqualToString:@"DELETECELL"]){
            HQStyleUserSearchEditCell * cell = [_tableView viewWithTag:1000+i];
            sty.danJia = cell.danJia.text;
            sty.huoHao = cell.huoHao.text;
            sty.m_material = cell.guiGe.text;
            if(_jiaGeHeSuanTag){
                sty.isOpenHeSuan = @"YES";
            }else{
                sty.isOpenHeSuan = @"NO";
            }
        }else{
            HQStyleUserSearchCell * cell = [_tableView viewWithTag:1000+i];
            sty.danJia = cell.danJia.text;
            sty.huoHao = cell.huoHao.text;
            if(_jiaGeHeSuanTag){
                sty.isOpenHeSuan = @"YES";
            }else{
                sty.isOpenHeSuan = @"NO";
            }
        }
        i++;
    }
    HQSearchObject *styleObject = [[HQSearchObject alloc]init];
    styleObject.itemTag = @"DELETECELL";
    if(_jiaGeHeSuanTag){
        styleObject.isOpenHeSuan = @"YES";
    }else{
        styleObject.isOpenHeSuan = @"NO";
    }
    [_dataSource addObject:styleObject];
    [_tableView reloadData];
}

/**
 删除按钮
 
 @param button 按钮
 */
-(void)deleteItem:(HQButton *)button{
    [_dataSource removeObject:button.dataInfo];
    [_tableView reloadData];
    [self textChange];
}

/**
 核算价格
 
 @param pickView pickerview
 @param resultString 结果输出
 */
-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    NSString *tempID = @"";
    NSString *tempName = @"";
    NSString *tempUnit = @"";
    for(NSDictionary *dic in _styleItem0Array){
        NSString *pattonStr = [NSString stringWithFormat:@"%@",dic[@"c_name"]];
        if([pattonStr isEqualToString:resultString]){
            tempID = dic[@"c_id"];
            tempName = dic[@"c_name"];
            tempUnit = dic[@"c_unit"];
            break;
        }
    }
    HQSearchObject *s = _currentCell.dataInfo;
    s.c_name = resultString;
    s.c_id = tempID;
    s.c_unit = tempUnit;
    _currentCell.unit.text = tempUnit;
    [_currentCell.mingCheng setTitle:resultString forState:UIControlStateNormal];
}

/*********************************************************************************************************/

/************************************************组件样式查询************************************************/
/**
 搜索 点击搜索需要输入区间
 
 @param sender 按钮
 */
- (IBAction)searchResultItem:(id)sender {
    if([self isInputEmpty:_chuangLianChiCun.text]){
        [self tostView:@"请输入窗帘尺寸"];
        return;
    }
    [self styleSearch];
}

/**
 组件样式查询
 */
-(void)styleSearch{
    [_dataSource removeAllObjects];
    [self.view endEditing:YES];
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam stylecomFin_unt:self.dataInfo[@"s_id"] withUnit:_chuangLianChiCun.text] withMethod:[HQMethod stylecomFin_unt] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            for(NSDictionary *dic in sucData[@"data"]){
                HQSearchObject *styleObject = [[HQSearchObject alloc]init];
                styleObject.itemTag = @"DEFAULT";
                styleObject.c_id = dic[@"c_id"];
                styleObject.c_name = dic[@"c_name"];
                styleObject.c_unit = dic[@"c_unit"];
                styleObject.m_material = dic[@"m_material"];
                styleObject.price = dic[@"price"];
                [_dataSource addObject:styleObject];
                if(_dataSource.count > 0){
                    _hesuanjiageButton.hidden = NO;
                    _searchButton2.hidden = NO;
                    _searchButton.hidden = YES;
                }
                [_tableView reloadData];
            }
        }else if([sucData[kStatus]integerValue] == 2){
            [self tostView:sucData[kInfo]];
            [_dataSource removeAllObjects];
            [_tableView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}
/********************************************************************************************************/
- (IBAction)lookBigImage:(id)sender {
    HQBigImageViewController *bigImageVc = [[HQBigImageViewController alloc]init];
    bigImageVc.imageURL = self.dataInfo[@"s_img"];
    [self presentViewController:bigImageVc animated:YES completion:nil];
}



- (HXDatePhotoToolManager *)toolManager {
    if (!_toolManager) {
        _toolManager = [[HXDatePhotoToolManager alloc] init];
    }
    return _toolManager;
}

- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
        _manager.configuration.openCamera = YES;
        _manager.configuration.lookLivePhoto = YES;
        _manager.configuration.photoMaxNum = 9;
        _manager.configuration.videoMaxNum = 1;
        _manager.configuration.maxNum = 10;
        _manager.configuration.videoMaxDuration = 500.f;
        _manager.configuration.saveSystemAblum = NO;
        //        _manager.configuration.reverseDate = YES;
        _manager.configuration.showDateSectionHeader = NO;
        _manager.configuration.selectTogether = NO;
        //        _manager.configuration.rowCount = 3;
        //        _manager.configuration.movableCropBox = YES;
        //        _manager.configuration.movableCropBoxEditSize = YES;
        //        _manager.configuration.movableCropBoxCustomRatio = CGPointMake(1, 1);
        _manager.configuration.requestImageAfterFinishingSelection = YES;
        __weak typeof(self) weakSelf = self;
        //        _manager.configuration.replaceCameraViewController = YES;
        _manager.configuration.shouldUseCamera = ^(UIViewController *viewController, HXPhotoConfigurationCameraType cameraType, HXPhotoManager *manager) {
            
            // 这里拿使用系统相机做例子
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            imagePickerController.delegate = (id)weakSelf;
            imagePickerController.allowsEditing = NO;
            NSString *requiredMediaTypeImage = ( NSString *)kUTTypeImage;
            NSString *requiredMediaTypeMovie = ( NSString *)kUTTypeMovie;
            NSArray *arrMediaTypes;
            if (cameraType == HXPhotoConfigurationCameraTypePhoto) {
                arrMediaTypes=[NSArray arrayWithObjects:requiredMediaTypeImage,nil];
            }else if (cameraType == HXPhotoConfigurationCameraTypeVideo) {
                arrMediaTypes=[NSArray arrayWithObjects:requiredMediaTypeMovie,nil];
            }else {
                arrMediaTypes=[NSArray arrayWithObjects:requiredMediaTypeImage, requiredMediaTypeMovie,nil];
            }
            [imagePickerController setMediaTypes:arrMediaTypes];
            // 设置录制视频的质量
            [imagePickerController setVideoQuality:UIImagePickerControllerQualityTypeHigh];
            //设置最长摄像时间
            [imagePickerController setVideoMaximumDuration:60.f];
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePickerController.navigationController.navigationBar.tintColor = [UIColor whiteColor];
            imagePickerController.modalPresentationStyle=UIModalPresentationOverCurrentContext;
            [viewController presentViewController:imagePickerController animated:YES completion:nil];
        };
    }
    return _manager;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    HXPhotoModel *model;
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        model = [HXPhotoModel photoModelWithImage:image];
        if (self.manager.configuration.saveSystemAblum) {
            [HXPhotoTools savePhotoToCustomAlbumWithName:self.manager.configuration.customAlbumName photo:model.thumbPhoto];
        }
    }else  if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        NSURL *url = info[UIImagePickerControllerMediaURL];
        NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
                                                         forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
        AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:opts];
        float second = 0;
        second = urlAsset.duration.value/urlAsset.duration.timescale;
        model = [HXPhotoModel photoModelWithVideoURL:url videoTime:second];
        if (self.manager.configuration.saveSystemAblum) {
            [HXPhotoTools saveVideoToCustomAlbumWithName:self.manager.configuration.customAlbumName videoURL:url];
        }
    }
    if (self.manager.configuration.useCameraComplete) {
        self.manager.configuration.useCameraComplete(model);
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)didNavBtnClick {
    if (self.manager.configuration.specialModeNeedHideVideoSelectBtn && !self.manager.configuration.selectTogether && self.manager.configuration.videoMaxNum == 1) {
        if (self.manager.afterSelectedVideoArray.count) {
            [self.view showImageHUDText:@"请先删除视频"];
            return;
        }
    }
    [self.photoView goPhotoViewController];
}

- (void)photoView:(HXPhotoView *)photoView changeComplete:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photos videos:(NSArray<HXPhotoModel *> *)videos original:(BOOL)isOriginal {
    //    NSSLog(@"所有:%ld - 照片:%ld - 视频:%ld",allList.count,photos.count,videos.count);
    //    NSSLog(@"所有:%@ - 照片:%@ - 视频:%@",allList,photos,videos);
    
    // 获取图片
    [self.toolManager getSelectedImageList:allList requestType:HXDatePhotoToolManagerRequestTypeOriginal success:^(NSArray<UIImage *> *imageList) {
        
    } failed:^{
        
    }];
    
    //    [HXPhotoTools selectListWriteToTempPath:allList requestList:^(NSArray *imageRequestIds, NSArray *videoSessions) {
    //        NSSLog(@"requestIds - image : %@ \nsessions - video : %@",imageRequestIds,videoSessions);
    //    } completion:^(NSArray<NSURL *> *allUrl, NSArray<NSURL *> *imageUrls, NSArray<NSURL *> *videoUrls) {
    //        NSSLog(@"allUrl - %@\nimageUrls - %@\nvideoUrls - %@",allUrl,imageUrls,videoUrls);
    //    } error:^{
    //        NSSLog(@"失败");
    //    }];
}

- (void)photoView:(HXPhotoView *)photoView imageChangeComplete:(NSArray<UIImage *> *)imageList {
    NSSLog(@"%@",imageList);
    _imageList = [[NSMutableArray alloc]initWithArray:imageList];
}

- (void)photoView:(HXPhotoView *)photoView deleteNetworkPhoto:(NSString *)networkPhotoUrl {
    NSSLog(@"%@",networkPhotoUrl);
}

- (void)photoView:(HXPhotoView *)photoView updateFrame:(CGRect)frame {
    NSSLog(@"%@",NSStringFromCGRect(frame));
    //    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, CGRectGetMaxY(frame) + kPhotoViewMargin);
    
}

- (void)photoView:(HXPhotoView *)photoView currentDeleteModel:(HXPhotoModel *)model currentIndex:(NSInteger)index {
    NSSLog(@"%@ --> index - %ld",model,index);
}

- (BOOL)photoViewShouldDeleteCurrentMoveItem:(HXPhotoView *)photoView {
    return self.needDeleteItem;
}
- (void)photoView:(HXPhotoView *)photoView gestureRecognizerBegan:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
    [UIView animateWithDuration:0.25 animations:^{
        self.bottomView.alpha = 0.5;
    }];
    NSSLog(@"长按手势开始了 - %ld",indexPath.item);
}
- (void)photoView:(HXPhotoView *)photoView gestureRecognizerChange:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
    CGPoint point = [longPgr locationInView:self.view];
    if (point.y >= self.bottomView.hx_y) {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.alpha = 1;
        }];
    }else {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.alpha = 0.5;
        }];
    }
    NSSLog(@"长按手势改变了 %@ - %ld",NSStringFromCGPoint(point), indexPath.item);
}
- (void)photoView:(HXPhotoView *)photoView gestureRecognizerEnded:(UILongPressGestureRecognizer *)longPgr indexPath:(NSIndexPath *)indexPath {
    CGPoint point = [longPgr locationInView:self.view];
    if (point.y >= self.bottomView.hx_y) {
        self.needDeleteItem = YES;
        [self.photoView deleteModelWithIndex:indexPath.item];
    }else {
        self.needDeleteItem = NO;
    }
    NSSLog(@"长按手势结束了 - %ld",indexPath.item);
    [UIView animateWithDuration:0.25 animations:^{
        self.bottomView.alpha = 0;
    }];
}


@end


